home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 49 / Amiga Format CD49 (2000-01-17)(Future Publishing)(GB)(Track 1 of 3)[!][issue 2000-02].iso / -serious- / misc / mmulib / c_sources / muscan.c < prev    next >
C/C++ Source or Header  |  1999-11-29  |  16KB  |  381 lines

  1. /*************************************************************************
  2.  ** mmu.library                                                         **
  3.  **                                                                     **
  4.  ** a system library for arbitration and control of the MC68K MMUs      **
  5.  **                                                                     **
  6.  ** © 1998,1999 THOR-Software, Thomas Richter                           **
  7.  ** No commercial use, reassembly, modification without prior, written  **
  8.  ** permission of the authors.                                          **
  9.  ** Including this library in any commercial software REQUIRES a        **
  10.  ** written permission and the payment of a small fee.                  **
  11.  **                                                                     **
  12.  ** This sample source demonstrates how to print out the mapping of the **
  13.  ** MMU library. It's here done for the default context                 **
  14.  *************************************************************************/
  15.  
  16. /// Includes
  17. #include <exec/types.h>
  18. #include <exec/memory.h>
  19. #include <dos/dos.h>
  20. #include <mmu/mmubase.h>
  21. #include <mmu/context.h>
  22. #include <mmu/config.h>
  23. #include <workbench/startup.h>
  24.  
  25. #include <proto/exec.h>
  26. #include <proto/mmu.h>
  27. #include <proto/dos.h>
  28. #include <proto/icon.h>
  29.  
  30. #include <string.h>
  31. ///
  32. /// Defines
  33. #define STRINGDATE "11.5.99"
  34. #define STRINGVERSION "40.2"
  35. #define TEMPLATE "TO/K"
  36.  
  37. #define OPT_TO  0
  38. #define OPT_WINDOW 1
  39. #define OPT_COUNT 2
  40. ///
  41. /// Statics
  42. struct MMUBase *MMUBase;
  43. struct DosLibrary *DOSBase;
  44. struct ExecBase *SysBase;
  45. struct Library *IconBase;
  46. ///
  47. /// Protos
  48. int __asm __saveds main(void);
  49. void ScanMMU(BPTR to);
  50. struct RDArgs *ReadTTArgs(struct WBStartup *msg,LONG args[],struct RDArgs **tmp);
  51. ///
  52.  
  53. char version[]="$VER: MuScan " STRINGVERSION " (" STRINGDATE ") © THOR";
  54.  
  55. /// main
  56. int __asm __saveds main(void)
  57. {
  58. LONG args[OPT_COUNT];
  59. struct RDArgs *rd,*myrd;
  60. struct Process *proc;
  61. int rc=20;
  62. LONG err;
  63. struct WBStartup *msg;
  64. BPTR oldout;
  65. struct MsgPort *oldconsole;
  66. BPTR out;
  67.  
  68.  
  69.         SysBase=*((struct ExecBase **)(4L));
  70.  
  71.         memset(args,0,sizeof(LONG)*OPT_COUNT);
  72.         /* Wait for the workbench startup, if any */
  73.         proc=(struct Process *)FindTask(NULL);
  74.  
  75.         if (!(proc->pr_CLI)) {
  76.                 WaitPort(&(proc->pr_MsgPort));
  77.                 msg=(struct WBStartup *)GetMsg(&(proc->pr_MsgPort));
  78.         } else  msg=NULL;
  79.  
  80.         if (DOSBase=(struct DosLibrary *)OpenLibrary("dos.library",37L)) {
  81.                 if (MMUBase=(struct MMUBase *)OpenLibrary("mmu.library",0L)) {
  82.  
  83.                         myrd=NULL;      /* reset the temporary ReadArgs */
  84.                         oldout=NULL;
  85.                         oldconsole=NULL;
  86.                         if (msg) {
  87.                                 oldout=SelectOutput(NULL);
  88.                                 oldconsole=SetConsoleTask(NULL);
  89.                                 rd=ReadTTArgs(msg,args,&myrd);
  90.                         } else  rd=ReadArgs(TEMPLATE,args,NULL);
  91.  
  92.                         if (rd) {
  93.                                 /* Argument parser worked, call main routine */
  94.                                 if (args[OPT_TO])       out=Open((char *)(args[0]),MODE_NEWFILE);
  95.                                 else                    out=Output();
  96.  
  97.                                 if (out) {
  98.                                         ScanMMU(out);
  99.                                         err=0;
  100.                                         if (args[OPT_TO])
  101.                                                 Close(out);
  102.                                 } else err=IoErr();
  103.  
  104.                                 FreeArgs(rd);
  105.                                 if (myrd) FreeDosObject(DOS_RDARGS,myrd);
  106.                                 if (msg)  Close(SelectOutput(NULL));
  107.                         } else  err=IoErr();
  108.  
  109.                         if (msg) {
  110.                                 SelectOutput(oldout);
  111.                                 SetConsoleTask(oldconsole);
  112.                         }
  113.  
  114.                         if (err<64) {
  115.                                 rc=err;
  116.                                 err=0;
  117.                         } else {
  118.                                 if (!msg) PrintFault(err,"MuScan failed");
  119.                                 rc=10;
  120.                         }
  121.                         SetIoErr(err);
  122.  
  123.                         CloseLibrary((struct Library *)MMUBase);
  124.                 } else PrintFault(ERROR_OBJECT_NOT_FOUND,"MuScan requires the mmu.library");
  125.                 CloseLibrary((struct Library *)DOSBase);
  126.         }
  127.  
  128.         return rc;
  129. }
  130. ///
  131. /// ReadTTArgs
  132. struct RDArgs *ReadTTArgs(struct WBStartup *msg,LONG args[],struct RDArgs **tmp)
  133. {
  134. struct WBArg *wbarg;
  135. struct DiskObject *dop;
  136. char **tt;                      /* ToolTypes array */
  137. char *wbstr;                    /* Our self-made workbench argument string */
  138. char *here;
  139. BPTR oldlock;
  140. ULONG len;
  141. struct RDArgs *rd=NULL,*myrd=NULL;
  142. LONG err=0;
  143. BPTR newout;
  144.  
  145.         if (IconBase=OpenLibrary("icon.library",37L)) {
  146.                 if (wbarg=msg->sm_ArgList) {
  147.                         /* use a project icon if there is one... */
  148.                         if (msg->sm_NumArgs > 1) wbarg++;
  149.  
  150.                         /* go into the directory */
  151.                         oldlock=CurrentDir(wbarg->wa_Lock);
  152.  
  153.                         if (dop=GetDiskObject(wbarg->wa_Name)) {
  154.                                 if (tt=dop->do_ToolTypes) {
  155.                                         /* Read a special tool type for the output window */
  156.  
  157.                                         /* Calc the size of the argument string */
  158.  
  159.                                         len = 3;        /* reserve space for SPC,LF,NUL */
  160.                                         while (*tt) {
  161.                                                 len += strlen(*tt)+1;   /* string, plus space */
  162.                                                 tt++;
  163.                                         }
  164.  
  165.                                         if (wbstr=AllocVec(len,MEMF_PUBLIC)) {
  166.                                                 /* Now copy the arguments into this string, one by one
  167.                                                    and check whether the argument string is still valid. */
  168.  
  169.                                                 tt=dop->do_ToolTypes;
  170.                                                 here=wbstr;
  171.                                                 do{
  172.                                                         *here='\0';                     /* terminate string */
  173.                                                         /* Check whether this tool type is
  174.                                                            commented out. Just ignore it in this case */
  175.                                                         if (*tt) {
  176.                                                                 if (**tt=='(' || **tt==';')
  177.                                                                         continue;
  178.  
  179.                                                                 strcpy(here,*tt);      /* Add TT string */
  180.                                                         }
  181.                                                         len=strlen(here);
  182.                                                         here[len]='\n';
  183.                                                         here[len+1]='\0';               /* terminate string */
  184.  
  185.                                                         /* Now try to ReadArg' this string */
  186.  
  187.                                                         /* release old arguments left over from last loop */
  188.                                                         if (rd) FreeArgs(rd);
  189.                                                         if (myrd) FreeDosObject(DOS_RDARGS,myrd);
  190.                                                         rd=NULL;
  191.                                                         memset(args,0,sizeof(LONG)*OPT_COUNT);
  192.  
  193.                                                         if (myrd=AllocDosObject(DOS_RDARGS,NULL)) {
  194.                                                                 /* Allocate and setup the ReadArgs source */
  195.                                                                 myrd->RDA_Source.CS_Buffer=wbstr;
  196.                                                                 myrd->RDA_Source.CS_Length=strlen(wbstr);
  197.  
  198.                                                                 if (rd=ReadArgs(TEMPLATE ",WINDOW/K",args,myrd)) {
  199.                                                                         /* Is this still valid? */
  200.                                                                         here[len]=' ';
  201.                                                                         here+=len+1;
  202.                                                                         /* if so, accept this argument and go on */
  203.                                                                 } else {
  204.                                                                         err=IoErr();
  205.                                                                         if (err==ERROR_NO_FREE_STORE) break;
  206.                                                                         else    err=0;  /* Ignore unknown or invalid arguments silently */
  207.                                                                 }
  208.                                                         } else {
  209.                                                                 err=ERROR_NO_FREE_STORE;
  210.                                                                 break;
  211.                                                         }
  212.                                                 }while(*tt++);
  213.  
  214.                                                 FreeVec(wbstr);
  215.                                         } else err=ERROR_NO_FREE_STORE;
  216.                                 } else err=ERROR_REQUIRED_ARG_MISSING; /* Huh, how should this happen ? */
  217.                                 FreeDiskObject(dop);
  218.                         } else err=IoErr();
  219.                         CurrentDir(oldlock);
  220.                 } else err=ERROR_REQUIRED_ARG_MISSING; /* This should not happen either */
  221.                 CloseLibrary(IconBase);
  222.         } else err=ERROR_OBJECT_NOT_FOUND;    /* This should not happen */
  223.  
  224.         /* Open an output stream */
  225.  
  226.         if (err==0) {
  227.                 if (newout=Open((args[OPT_WINDOW])?((char *)args[OPT_WINDOW]):("NIL:"),MODE_NEWFILE)) {
  228.                         SelectOutput(newout);
  229.                         /* Hack in the output console. Well, well... */
  230.                         SetConsoleTask(((struct FileHandle *)(BADDR(newout)))->fh_Type);
  231.                 } else err=IoErr();
  232.         }
  233.  
  234.         if (err) {
  235.                 if (rd)   FreeArgs(rd);
  236.                 if (myrd) FreeDosObject(DOS_RDARGS,myrd);
  237.                 SetIoErr(err);
  238.                 rd=NULL;
  239.                 myrd=NULL;
  240.         }
  241.  
  242.         *tmp=myrd;
  243.         return rd;
  244. }
  245. ///
  246. /// ScanMMU
  247. void ScanMMU(BPTR to)
  248. {
  249. struct MinList *list;
  250. struct MappingNode *mn;
  251.  
  252.         FPrintf(to,version+6);
  253.         FPrintf(to,"\n\n");
  254.  
  255.         switch (GetMMUType()) {
  256.                 case MUTYPE_NONE:
  257.                         FPrintf(to,"No MMU available.\n");
  258.                         break;
  259.                 case MUTYPE_68851:
  260.                         FPrintf(to,"68851 MMU detected.\n");
  261.                         break;
  262.                 case MUTYPE_68030:
  263.                         FPrintf(to,"68030 MMU detected.\n");
  264.                         break;
  265.                 case MUTYPE_68040:
  266.                         FPrintf(to,"68040 MMU detected.\n");
  267.                         break;
  268.                 case MUTYPE_68060:
  269.                         FPrintf(to,"68060 MMU detected.\n");
  270.                         break;
  271.         };
  272.  
  273.         /* Read now the page size of the MMU for the default
  274.            (public) context */
  275.  
  276.         FPrintf(to,"MMU page size is 0x%lx bytes.\n\n",GetPageSize(NULL));
  277.  
  278.  
  279.         /* Get the mapping of the default context */
  280.         list=GetMapping(NULL);
  281.  
  282.         /* next, we print it out. Usually, you should
  283.            make a copy first since the context is now
  284.            blocked, i.e. nobody else will be allowed to
  285.            make changes to it. */
  286.  
  287.         FPrintf(to,"Memory map:\n");
  288.  
  289.         for(mn=(struct MappingNode *)(list->mlh_Head);mn->map_succ;mn=mn->map_succ) {
  290.  
  291.                 FPrintf(to,"0x%08lx - 0x%08lx ",mn->map_Lower,mn->map_Higher);
  292.  
  293.                 if (mn->map_Properties & MAPP_WRITEPROTECTED)
  294.                         FPrintf(to,"WriteProtected ");
  295.  
  296.                 if (mn->map_Properties & MAPP_USED)
  297.                         FPrintf(to,"U ");
  298.  
  299.                 if (mn->map_Properties & MAPP_MODIFIED)
  300.                         FPrintf(to,"M ");
  301.  
  302.                 if (mn->map_Properties & MAPP_GLOBAL)
  303.                         FPrintf(to,"Global ");
  304.  
  305.                 if (mn->map_Properties & MAPP_TRANSLATED)
  306.                         FPrintf(to,"TT ");
  307.  
  308.                 if (mn->map_Properties & MAPP_ROM)
  309.                         FPrintf(to,"ROM ");
  310.  
  311.                 if (mn->map_Properties & MAPP_USERPAGE0)
  312.                         FPrintf(to,"UP0 ");
  313.  
  314.                 if (mn->map_Properties & MAPP_USERPAGE1)
  315.                         FPrintf(to,"UP1 ");
  316.  
  317.                 if (mn->map_Properties & MAPP_CACHEINHIBIT)
  318.                         FPrintf(to,"CacheInhibit ");
  319.  
  320.                 if (mn->map_Properties & MAPP_IMPRECISE)
  321.                         FPrintf(to,"Imprecise ");
  322.  
  323.                 if (mn->map_Properties & MAPP_NONSERIALIZED)
  324.                         FPrintf(to,"NonSerial ");
  325.  
  326.                 if (mn->map_Properties & MAPP_COPYBACK)
  327.                         FPrintf(to,"CopyBack ");
  328.  
  329.                 if (mn->map_Properties & MAPP_SUPERVISORONLY)
  330.                         FPrintf(to,"SuperOnly ");
  331.  
  332.                 if (mn->map_Properties & MAPP_BLANK)
  333.                         FPrintf(to,"Blank ");
  334.  
  335.                 if (mn->map_Properties & MAPP_SHARED)
  336.                         FPrintf(to,"Shared ");
  337.  
  338.                 if (mn->map_Properties & MAPP_SINGLEPAGE)
  339.                         FPrintf(to,"Single ");
  340.  
  341.                 if (mn->map_Properties & MAPP_REPAIRABLE)
  342.                         FPrintf(to,"Repairable ");
  343.  
  344.                 if (mn->map_Properties & MAPP_IO)
  345.                         FPrintf(to,"I/O space ");
  346.  
  347.                 if (mn->map_Properties & MAPP_USER0)
  348.                         FPrintf(to,"U0 ");
  349.  
  350.                 if (mn->map_Properties & MAPP_USER1)
  351.                         FPrintf(to,"U1 ");
  352.  
  353.                 if (mn->map_Properties & MAPP_USER2)
  354.                         FPrintf(to,"U2 ");
  355.  
  356.                 if (mn->map_Properties & MAPP_USER3)
  357.                         FPrintf(to,"U3 ");
  358.  
  359.                 if (mn->map_Properties & MAPP_INVALID)
  360.                         FPrintf(to,"Invalid (0x%08lx) ",mn->map_un.map_UserData);
  361.  
  362.                 if (mn->map_Properties & MAPP_SWAPPED)
  363.                         FPrintf(to,"Swapped (0x%08lx) ",mn->map_un.map_UserData);
  364.  
  365.                 if (mn->map_Properties & MAPP_REMAPPED)
  366.                         FPrintf(to,"Remapped to 0x%08lx ",mn->map_un.map_Delta+mn->map_Lower);
  367.  
  368.                 if (mn->map_Properties & MAPP_BUNDLED)
  369.                         FPrintf(to,"Bundled to 0x%08lx ",mn->map_un.map_Page);
  370.  
  371.                 if (mn->map_Properties & MAPP_INDIRECT)
  372.                         FPrintf(to,"Indirect at 0x%08lx ",mn->map_un.map_Descriptor);
  373.  
  374.                 FPrintf(to,"\n");
  375.         }
  376.  
  377.         ReleaseMapping(NULL,list);
  378. }
  379. ///
  380.  
  381.